package edu.sctu.wangxiaoqiang.day20150615;

import java.io.*;
import java.nio.Buffer;
import java.util.Scanner;

/**
 * Created by user25 on 2015/6/15.
 */
public class BufferReader {
    public static final String WEN_JIAN = "D:\\Buffer";
    public static final String NEWWEN_JIAN = WEN_JIAN+"\\new.txt";

    /*
    创建文件夹
     */
    public void setFile(){
        File file1 = new File(WEN_JIAN);
        if(!file1.exists()){
            file1.mkdir();
            System.out.println("文件夹创建成功！");
        }else {
            System.out.println(file1+" 文件夹已经存在！");
        }
    }

    /*
    创建文件
     */
    public void setNewFile(){
        File file2 = new File(NEWWEN_JIAN);
        if(!file2.exists()){
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件创建成功！");
        }else {
            System.out.println(file2+"该文件已经存在！");
        }
    }
    /*
    添加内容
     */
    public void printWriter(){
        try {
            PrintWriter printWriter = new PrintWriter(NEWWEN_JIAN);
            System.out.println("请输入内容：");
            printWriter.print(new Scanner(System.in).nextLine());
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
    读取字符流文件
     */
    public void fileStreamInputAndOutpu(){
        int count = 0;
        byte[] b = new byte[2];
        try {
            FileInputStream fileInputStream = new FileInputStream(NEWWEN_JIAN);
            FileOutputStream fileOutputStream = new FileOutputStream(WEN_JIAN+"\\SDFSDF.TXT");
            try {
                while ((count = fileInputStream.read()) !=-1 ){
                  fileOutputStream.write(count);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    /*
   读取字节流  buffer用法
     */
    public void fileReaderAndWriter(){
        int num = 0;
        char[] c = new char[12];
        try {
            FileReader fileReader = new FileReader(NEWWEN_JIAN);
            FileWriter fileWriter = new FileWriter(WEN_JIAN+"\\123.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            try {
                while (bufferedReader.ready()){
                    String inStr = bufferedReader.readLine();
                    System.out.println("行的内容："+inStr);
                    bufferedWriter.write(inStr);
                    bufferedWriter.newLine();
                }
                bufferedReader.close();
                bufferedWriter.close();
                System.out.println("成功创建123.txt文件");
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileReader.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }




}
